<?php
    sleep(2);
    header('Access-Control-Allow-Origin:*');
    // header("Access-Control-Allow-Origin:http://192.168.206.10:8085");
    header("Access-Control-Allow-Origin:http://localhost:8085");
    
    header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept');
    if(isset($_POST['username']) && 
        isset($_POST["userpsw"]) &&
        isset($_POST["usertel"])
        ) {
        // 插入数据库 之前 需要判断该用户是否已经注册
        require "./extends/config.php";
        require "./extends/Model.class.php";
        $userModel = new Model("b_user");
        $username = $_POST['username'];
        $selectResult = $userModel->where("user_name='$username'")->select();
        if($selectResult){
            // 该用户名已经存在 无法注册
            $result['code'] = 1;
            $result['data'] = "用户名已经注册 请更换用户名";
            echo json_encode($result);
        }else {
            // 插入数据到数据库
            $newUser['user_name'] = $_POST['username'];
            $newUser['user_password'] = $_POST['userpsw'];
            $newUser['user_tel'] = $_POST['usertel'];
            $newUser['user_sex'] = "m";

            $addResult = $userModel->add($newUser);
            if($addResult>0){
                $result['code'] = 0;
                $result['data'] = "用户注册成功!";
                echo json_encode($result);
            }else {
                $result['code'] = 2;
                $result['data'] = "用户名注册失败 请稍后在尝试";
                echo json_encode($result);
            }
        }
    }else {
        $result['code'] = 3;
        $result['data'] = "no user params";
        echo json_encode($result);
    }